package org.jmat.batch.listeners;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.util.StopWatch;


public class JobListenerSupport implements JobExecutionListener{
	
	private final Log log  = LogFactory.getLog(JobListenerSupport.class);
	StopWatch watch = new StopWatch();

	public void afterJob(JobExecution jobExecution) {
		watch.stop();
		if (log.isDebugEnabled()) {
			log.debug("End job " + jobExecution.getId());
			log.debug("Total time : " + watch.getTotalTimeSeconds() + " sec");
		}
	}

	public void beforeJob(JobExecution jobExecution) {
		watch.start();
		if (log.isDebugEnabled()) {
			log.debug("Begin job " + jobExecution.getId());
		}
	}

	public void onError(JobExecution jobExecution, Throwable throwable) {
		log.error("Error in job " + jobExecution.getId());
	}

	public void onInterrupt(JobExecution jobExecution) {
		if (log.isInfoEnabled()) {
			log.info("Interrup job " + jobExecution.getId());
		}	
	}

}
